package com.hg.util.common;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.jdiy.util.Fs;

import com.hg.cache.FIFOCache;

public class CacheFileUtil
{
	public static String cache_file_url = "public/runtime/";
	/**
	 * @param request
	 * @param file_url 		public/runtime/  后面那部分,比如 config/config.json
	 * @param data_str		需要存储的字符串
	 * @return
	 */
	public static boolean generateFile(HttpServletRequest request,String file_url,String data_str){
		String fileUrl = cache_file_url+file_url;
		
		File file=new File(request.getRealPath(fileUrl));
		
		try
		{
			Fs.writeFile(file, data_str, "utf-8");
			return true;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	/**
     * 判断缓存的json文件是否存在
     * @return
     */
    public static boolean isExistsFile(HttpServletRequest request,String file_url){
    	String fileUrl = cache_file_url+file_url;
		
		File file=new File(request.getRealPath(fileUrl));
    	return file.exists();
    }
    
    /**
     * 读取json文件
     * @return
     */
    public static String readFile(HttpServletRequest request,String file_url){
    	String fileUrl = cache_file_url+file_url;
		
		File file=new File(request.getRealPath(fileUrl));
    	try
		{
			return Fs.readFile(file,"utf-8");
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return null;
		}
    }
    
    /**
     * 更新移动端系统配置
     * @param cache
     */
    public static void updateMConfig(FIFOCache<String, String> cache){
    	
    }
}
